package org.jeecg.modules.oms.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.oms.entity.DesignFile;
import org.jeecg.modules.oms.entity.DesignFileProcessLog;
import org.jeecg.modules.oms.mapper.DesignFileMapper;
import org.jeecg.modules.oms.mapper.DesignFileProcessLogMapper;
import org.jeecg.modules.oms.service.IDesignFileProcessLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 设计文件处理日志Service实现
 */
@Slf4j
@Service
public class DesignFileProcessLogServiceImpl extends ServiceImpl<DesignFileProcessLogMapper, DesignFileProcessLog> implements IDesignFileProcessLogService {
    
    @Autowired
    private DesignFileMapper designFileMapper;
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean recordProcessLog(DesignFileProcessLog processLog) {
        if (processLog == null) {
            throw new JeecgBootException("处理日志信息不能为空");
        }
        
        if (StringUtils.isBlank(processLog.getFileId())) {
            throw new JeecgBootException("文件ID不能为空");
        }
        
        // 验证文件是否存在
        DesignFile file = designFileMapper.selectById(processLog.getFileId());
        if (file == null) {
            throw new JeecgBootException("未找到对应文件");
        }
        
        // 设置处理时间
        if (processLog.getProcessTime() == null) {
            processLog.setProcessTime(new Date());
        }
        
        // 设置创建时间
        processLog.setCreateTime(new Date());
        
        return save(processLog);
    }
    
    @Override
    public List<DesignFileProcessLog> getProcessLogsByFileId(String fileId) {
        if (StringUtils.isBlank(fileId)) {
            throw new JeecgBootException("文件ID不能为空");
        }
        
        return baseMapper.getProcessLogsByFileId(fileId);
    }
    
    @Override
    public DesignFileProcessLog getLatestProcessLogByFileId(String fileId) {
        if (StringUtils.isBlank(fileId)) {
            throw new JeecgBootException("文件ID不能为空");
        }
        
        return baseMapper.getLatestProcessLogByFileId(fileId);
    }
} 